import { ref } from 'vue';
import moment from 'moment';

interface CompareOption<T> {
  name: string;
  symbol: string;
  compare: (a: T, b: any) => boolean;
}

export const strCompareOptions: CompareOption<string>[] = [
  {
    name: '等于',
    symbol: 'EQ',
    compare: (a, b) => {
      console.log('str--->', a, b[0]);
      console.log(a == b[0]);
      return a == b[0];
    },
  },
  { name: '不等于', symbol: 'NEQ', compare: (a, b) => a != b[0] },
  { name: '为其之一', symbol: 'IN', compare: (a, b) => (b || []).includes(a) },
  { name: '不为其之一', symbol: 'NIN', compare: (a, b) => !(b || []).includes(a) },
  { name: '含有', symbol: 'LIKE', compare: (a, b) => (b || '').includes(a) },
];

export const strArrCompareOptions: CompareOption<string[]>[] = [
  { name: '包含', symbol: 'HS', compare: (a, b) => checkElementsExistInArray(b, a) },
  { name: '不包含', symbol: 'NHS', compare: (a, b) => !checkElementsExistInArray(b, a) },
];

export const numCompareOptions: CompareOption<number>[] = [
  { name: '大于', symbol: 'GT', compare: (a, b) => a > b },
  { name: '小于', symbol: 'LT', compare: (a, b) => a < b },
  { name: '等于', symbol: 'EQ', compare: (a, b) => a == b },
  { name: '不等于', symbol: 'NEQ', compare: (a, b) => a != b },
  { name: '大于等于', symbol: 'GT_EQ', compare: (a, b) => a >= b },
  { name: '小于等于', symbol: 'LT_EQ', compare: (a, b) => a <= b },
  { name: '在内或相等', symbol: 'IN_EQ', compare: (a, b) => a >= b[0] && a <= b[1] },
  { name: '在内或不等', symbol: 'IN_NEQ', compare: (a, b) => a == b },
  { name: '为其之一', symbol: 'IN', compare: (a, b) => (b || []).includes(a) },
  { name: '不为其之一', symbol: 'NIN', compare: (a, b) => !(b || []).includes(a) },
];

export const orgCompareOptions: CompareOption<any>[] = [
  { name: '是', symbol: 'EQ', compare: (a, b) => a[0].id == b[0].id },
  { name: '不是', symbol: 'NEQ', compare: (a, b) => a[0].id != b[0].id },
  { name: '为其之一', symbol: 'IN', compare: (a, b) => checkElementsExistInArray(a, b, 'id') },
  { name: '含有', symbol: 'HS', compare: (a, b) => checkElementsExistInArray(b, a, 'id') },
  { name: '不含有', symbol: 'NHS', compare: (a, b) => !checkElementsExistInArray(b, a, 'id') },
];

export const timeCompareOptions: CompareOption<string>[] = [
  { name: '在之前', symbol: 'LT', compare: (a, b) => moment(a).isBefore(moment(b[0])) },
  { name: '在之后', symbol: 'GT', compare: (a, b) => moment(a).isAfter(moment(b[0])) },
  {
    name: '在其中',
    symbol: 'IN',
    compare: (a, b) => moment(b[0]).isBefore(moment(a)) && moment(b[1]).isAfter(moment(a)),
  },
];

export const timeArrCompareOptions: CompareOption<string[]>[] = [
  {
    name: '包含',
    symbol: 'HS',
    compare: (a, b) => moment(a[0]).isBefore(moment(b[0])) && moment(a[1]).isAfter(moment(b[0])),
  },
  {
    name: '不包含',
    symbol: 'NHS',
    compare: (a, b) => moment(b[0]).isBefore(moment(a[0])) || moment(b[0]).isAfter(moment(a[1])),
  },
];

function getCompareFuncs() {
  const compareFuncs = {
    strCompare: {} as Record<string, (a: string, b: any) => boolean>,
    strArrCompare: {} as Record<string, (a: string[], b: any) => boolean>,
    numCompare: {} as Record<string, (a: number, b: any) => boolean>,
    orgCompare: {} as Record<string, (a: any, b: any) => boolean>,
    timeCompare: {} as Record<string, (a: string, b: any) => boolean>,
    timeArrCompare: {} as Record<string, (a: string[], b: any) => boolean>,
  };

  strCompareOptions.forEach((v) => (compareFuncs.strCompare[v.symbol] = v.compare));
  strArrCompareOptions.forEach((v) => (compareFuncs.strArrCompare[v.symbol] = v.compare));
  numCompareOptions.forEach((v) => (compareFuncs.numCompare[v.symbol] = v.compare));
  orgCompareOptions.forEach((v) => (compareFuncs.orgCompare[v.symbol] = v.compare));
  timeCompareOptions.forEach((v) => (compareFuncs.timeCompare[v.symbol] = v.compare));
  timeArrCompareOptions.forEach((v) => (compareFuncs.timeArrCompare[v.symbol] = v.compare));

  return compareFuncs;
}

export function checkElementsExistInArray(A: any[], B: any[], key?: string) {
  if (A.length === 0 || B.length === 0) {
    return false;
  }
  for (let i = 0; i < A.length; i++) {
    let found = false;
    for (let j = 0; j < B.length; j++) {
      if (key) {
        if (A[i][key] == B[j][key]) {
          found = true;
          break;
        }
      } else {
        if (A[i] == B[j]) {
          found = true;
          break;
        }
      }
    }
    if (!found) {
      return false;
    }
  }
  return true;
}

export const CompareFuncs = getCompareFuncs();
